package com.sunfeng.netty.atguigu.codec4;

import com.google.protobuf.TextFormat;
import com.sunfeng.netty.atguigu.codec3.SubScribeReq;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sunfeng
 * @date 2021/05/19 17:16
 **/
@Slf4j
public class SubReqProtoClientHandler extends SimpleChannelInboundHandler<SubscribeRespProto.SubscribeResp> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i++) {
            ctx.writeAndFlush(subReq(i));
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SubscribeRespProto.SubscribeResp msg) throws Exception {
        log.info("接受到的数据为：" + TextFormat.printToUnicodeString(msg) );
    }

    private SubScribeReqProto.SubScribeReq subReq(int i) {
        SubScribeReqProto.SubScribeReq.Builder builder = SubScribeReqProto.SubScribeReq.newBuilder();
        builder.setAddress("山东菏泽曹县NP,666");
        builder.setPhoneNumber("18354680214");
        builder.setProductName("Netty 权威指南");
        builder.setSubReqID(i);
        builder.setUserName("SunFeng");
        return builder.build();

    }
}
